import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { Message } from 'element-ui'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    isCheckPrice: '',
    showWarehouseInfo: 1,
    roles: [],
    userInfo: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_WAREHOUSEINFO: (state, info) => {
    state.showWarehouseInfo = info
    // state.showWarehouseInfo = 0
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_CHECKPRICE: (state, avatar) => {
    state.isCheckPrice = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_USERINFO: (state, data) => {
    state.userInfo = data
  }
}

const actions = {
  // user login
  login ({ commit }, userInfo) {
    const { userName, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ userName: userName.trim(), password: password })
        .then(response => {
          if (response.code === 200) {
            // commit('SET_TOKEN', 'token')
            // setToken('token')
            commit('SET_TOKEN', response.data)
            setToken(response.data)

            commit('SET_NAME', userName)
            resolve()
          } else {
            Message.error(response.mass)
            reject()
          }
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  login2 ({ commit }, websiteToken) {
    return new Promise((resolve, reject) => {
      login(websiteToken)
        .then(response => {
          if (response.code === 200) {
            commit('SET_TOKEN', response.data)
            setToken(response.data)
            resolve()
          } else {
            Message.error(response.mass)
            reject()
          }
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo ({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then(response => {
          if (!response) {
            return reject('Verification failed, please Login again.')
          }
          console.log('userdata', response)
          const { isCheckPrice, customerNameShort, showWarehouseInfo } = response

          const roles = isCheckPrice ? ['admin'] : ['editor']
          response.roles = roles
          // localStorage.setItem('roles', JSON.stringify(roles))
          commit('SET_USERINFO', response)
          commit('SET_ROLES', roles)
          commit('SET_NAME', customerNameShort)
          commit('SET_WAREHOUSEINFO', showWarehouseInfo)
          commit('SET_CHECKPRICE', isCheckPrice)
          // commit('SET_AVATAR', avatar)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  clearRoles ({ commit }) {
    commit('SET_ROLES', [])
  },

  // user logout
  logout ({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      resetRouter()
      localStorage.removeItem('vuex')
      localStorage.clear()
      commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken ({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
